<?php

class WebActiveRecord extends BaseActiveRecord
{
    public $no_record_log = 0;
    public $is_record = true;
    public $column_name = null;
    public $module_id = null;
    public $before_data = array();
    public $after_data = array();
    public $modules = array();


    public static $static_modules = array(
        'adv' => '广告',
        'advcategory' => '广告分类',
        'article' => '文章',
        'comment'=>'商品评论',
        'manager' => '管理员',

        'user' => '用户',
        'order' => '订单',
        'config' => '系统设置',
        'package'=>'版本',
        );

    public function getDbConnection()
    {
        self::$db = Yii::app()->db_web;
        if(self::$db instanceof CDbConnection)
            return self::$db;
        else
            throw new CDbException(Yii::t('yii','Active Record requires a "db" CDbConnection application component.'));
    }

    /**
     * 保存前
     * @return [type] [description]
     */
    public function beforeSave()
    {

        $schema = $this->getTableSchema();
        if ($this->getIsNewRecord()) {
            if (in_array('ctime', array_keys($schema->columns))) {
                $this->ctime = time();
            }
            if (in_array('mtime', array_keys($schema->columns))) {
                $this->mtime = time();
            }
        } else {
            if (in_array('mtime', array_keys($schema->columns))) {
                $this->mtime = time();
            }
        }
        return true;
    }
    public function afterFind()
    {
        $this->modules = self::$static_modules;
        if (!$this->no_record_log && !is_null($this->column_name) && !is_null($this->module_id)) {
            if ($this->getIsNewRecord() == false) {
                $this->before_data = $this->attributes;
            }
        }
    }

    public function afterSave()
    {
        $this->modules = self::$static_modules;
        // 如果是命令行调用
        if (Yii::app() instanceof CConsoleApplication) {
            return;
        }
        if (!is_null($this->column_name) && !is_null($this->module_id) && $this->is_record) {
            $this->after_data = $this->attributes;
            $logdata = array();
            $logdata['user_id'] = Yii::app()->user->getId();
            if ($logdata['user_id'] == 0 || !is_numeric($logdata['user_id'])) {
                return;
            }
            if (!empty($this->modules[$this->module_id])) {
                if (empty($this->before_data) && !empty($this->after_data)) {
                    $logdata['log_msg'] = '模块：'. $this->modules[$this->module_id] . '; ' . Yii::app()->user->getName().'新增'. $this->after_data[$this->column_name];
                    $logdata['extended'] = array('new' => $this->after_data);
                    $logdata['type'] = TmsOperationLog::OPTION_TYPE_ADD;
                } else {
                    if (!empty($this->after_data['is_deleted']) && $this->after_data['is_deleted'] == 1) {
                        $logdata['log_msg'] = '模块：'. $this->modules[$this->module_id] . '; ' . Yii::app()->user->getName().'删除'. $this->after_data[$this->column_name];
                        $logdata['extended'] = $this->before_data;
                        $logdata['type'] = TmsOperationLog::OPTION_TYPE_DELETED;
                    } else {
                        $logdata['log_msg'] = '模块：'. $this->modules[$this->module_id] . '; ' . Yii::app()->user->getName().'修改'. $this->after_data[$this->column_name];
                        $logdata['extended'] = array('old' => $this->before_data, 'new' => $this->after_data);
                        $logdata['type'] = TmsOperationLog::OPTION_TYPE_UPDATE;
                    }
                }
                $logdata['module_id'] = $this->module_id;
                TmsOperationLog::model()->add($logdata);
            }
        }

    }
}
